package com.etc.service;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户业务层
 */
public class UserService implements UserDao {


    private SqlSessionFactory sqlSessionFactory;

    public UserService(SqlSessionFactory sqlSessionFactory){
     this.sqlSessionFactory = sqlSessionFactory;
    }


    /**
     * 根据id 查询用户
     * @param id  用户id
     * @return
     */
    @Override
    public User findbyid(Integer id) {
        // 数据库会话实例
        SqlSession sqlSession = null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息

            User user1 = new User();
            ArrayList<Integer> integers = new ArrayList<>();
            integers.add(3);
            integers.add(4);
//            user1.setIds(integers);
            List<User> user = sqlSession.selectList("findbyid",user1);
           return user.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Override
    public Integer save(User user) {
        return null;
    }

    @Override
    public Integer deletebyid(Integer id) {
        return null;
    }

    @Override
    public Integer updatebyid(User user) {
        return null;
    }

    @Override
    public List<User> getUserOrderList() {
        // 数据库会话实例
        SqlSession sqlSession = null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
//            user1.setIds(integers);
            List<User> user = sqlSession.selectList("getUserOrderList");
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
